package com.eam.common.core.redis;

import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Component
public class SRTemplateCache {

    private final StringRedisTemplate stringRedisTemplate;

    @Autowired
    public SRTemplateCache(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }


    public void set(final String key, final String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public void set(final String key, final String value, final Integer timeout, final TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    public void set(final String key, final Object value) {
        String jsonStr = JSONUtil.toJsonStr(value);
        if (jsonStr != null) {
            stringRedisTemplate.opsForValue().set(key, jsonStr);
        }
    }

    public void set(final String key, final Object value, final Integer timeout, final TimeUnit timeUnit) {
        String jsonStr = JSONUtil.toJsonStr(value);
        if (jsonStr != null) {
            stringRedisTemplate.opsForValue().set(key, jsonStr, timeout, timeUnit);
        }
    }

    /**
     * @param key     key
     * @param timeout 超时时间
     * @return boolean
     * @description: 设置key的超时时间, 当前默认时间单位为s
     * @author tzf
     * @date: 2023/11/4 10:43
     */

    public boolean expire(final String key, final long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * @param key     key
     * @param timeout 超时时间
     * @param unit    时间单位
     * @return boolean
     * @description: 设置key的超时时间
     * @author tzf
     * @date: 2023/11/4 10:43
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
        return Boolean.TRUE.equals(stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS));
    }

    public long getExpire(final String key) {
        return stringRedisTemplate.getExpire(key);
    }

    /**
     * @param key 键
     * @return java.lang.Boolean
     * @description: 判断key是否存在
     * @author tzf
     * @date: 2023/11/4 10:48
     */
    public Boolean hasKey(final String key) {
        return stringRedisTemplate.hasKey(key);
    }

    /**
     * @param key 键
     * @param c   类
     * @return T
     * @description: 获取key的值, 可指定class类, 进行Bean转换,注意:如果是map集合则需要自己手动调用下面重载的get方法获取jsonStr进行转换
     * @author tzf
     * @date: 2023/11/4 10:52
     */

    public <T> T get(final String key, final Class<T> c) {
        String keyValue = stringRedisTemplate.opsForValue().get(key);
        if (keyValue == null) return null;
        return JSONUtil.toBean(keyValue, c);
    }

    /**
     * @param key 键
     * @return java.lang.String
     * @description: 获取key的值, 以jsonStr格式
     * @author tzf
     * @date: 2023/11/4 10:57
     */

    public String get(final String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * @param key 键
     * @return boolean
     * @description: 删除单个key
     * @author 86187
     * @date: 2023/11/4 11:00
     */

    public boolean delete(final String key) {
        return Boolean.TRUE.equals(stringRedisTemplate.delete(key));
    }

    public boolean delete(final Collection<String> collection) {
        return stringRedisTemplate.delete(collection) > 0;
    }
}
